home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / st_read.c < prev    next >
C/C++ Source or Header  |  1995-05-11  |  13KB  |  535 lines

  1. /* st_read.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: st_read.c,v 4.18 1995/05/11 12:28:50 espie Exp espie $
  6.  * $Log: st_read.c,v $
  7.  * Revision 4.18  1995/05/11  12:28:50  espie
  8.  * Bug correction, case for effects.
  9.  *
  10.  * Revision 4.17  1995/03/03  14:24:28  espie
  11.  * 15 colors.
  12.  *
  13.  * Revision 4.16  1995/03/01  15:24:51  espie
  14.  * Stuff with scaling a bit cleaned up (though not perfect yet).
  15.  *
  16.  * Revision 4.15  1995/02/24  15:36:39  espie
  17.  * Need to set all pblock events to 0 for correct clean up.
  18.  *
  19.  * Revision 4.14  1995/02/21  21:13:16  espie
  20.  * Cleaned up source. Moved minor pieces of code around.
  21.  *
  22.  * Revision 4.13  1995/02/21  17:54:32  espie
  23.  * Internal problem: buggy RCS. Fixed logs.
  24.  *
  25.  * Revision 4.11  1995/02/20  22:53:31  espie
  26.  * Added missing parameters everywhere.
  27.  *
  28.  * Revision 4.10  1995/02/20  22:28:50  espie
  29.  * 8 channels.
  30.  *
  31.  * Revision 4.9  1995/02/20  16:49:58  espie
  32.  * Added song type.
  33.  *
  34.  * Revision 4.7  1995/02/06  14:50:47  espie
  35.  * Changed sample_info.
  36.  *
  37.  * Revision 4.6  1995/02/01  17:14:54  espie
  38.  * Scaled volume.
  39.  *
  40.  * Revision 4.0  1994/01/11  17:56:51  espie
  41.  * Use new open.
  42.  * Added lots of checks for malloc, plus count of bytes read.
  43.  * Amiga support.
  44.  * Added other commands (numerous).
  45.  * Checks for finetune ?
  46.  *
  47.  * Revision 2.16  1992/11/17  17:06:25  espie
  48.  * fix_xxx for better speed.
  49.  * Added some sample information in the dump.
  50.  * Added transpose feature.
  51.  * Feature fix: length 1 sample should be empty.
  52.  * Corrected repeat length problems concerning badly formed files,
  53.  * added signature checking for new tracker files.
  54.  * Corrected small problem with repeat being too short.
  55.  * Coded error types. More amiga specific stuff.
  56.  *
  57.  * Revision 1.17  1991/11/17  16:30:48  espie
  58.  * Rationnalized error recovery.
  59.  * There was a bug: you could try to deallocate
  60.  * stuff in no-noland. Also, strings never got
  61.  * to be freed.
  62.  * Centralized error control to error_song.
  63.  * Added a new test on length, aborts most modules now.
  64.  * Maybe should say it as well.
  65.  * Added checkpoints for early return if file too short.
  66.  * Added memory recovery and error control.
  67.  * Suppressed ! warning for bad note.
  68.  * Added note support.
  69.  * Corrected length and rep_length/rep_offset
  70.  * which are given in words and should be converted to
  71.  * bytes.
  72.  */
  73.  
  74. #include "defs.h"
  75.  
  76. #include <ctype.h>
  77. #include <assert.h>
  78.  
  79. #include "extern.h"
  80. #include "song.h"
  81. #include "channel.h"
  82.  
  83.  
  84. LOCAL int color;
  85. ID("$Id: st_read.c,v 4.18 1995/05/11 12:28:50 espie Exp espie $")
  86.  
  87. /***
  88.  ***    Low level st-file access routines 
  89.  ***/
  90.  
  91. #define MAX_LEN 50
  92. /* s = getstring(f, len):
  93.  * gets a soundtracker string from file f.
  94.  * I.e, it is a fixed length string terminated
  95.  * by a 0 if too short. Length should be
  96.  * smaller than MAX_LEN.
  97.  */
  98. LOCAL char *getstring(f, len)
  99. struct exfile *f;
  100. int len;
  101.    {
  102.    static char s[MAX_LEN];
  103.    char *new;
  104.    int i;
  105.         
  106.    assert(len < MAX_LEN);
  107.    for (i = 0; i < len; i++)
  108.       s[MIN(i, MAX_LEN - 1)] = getc_file(f);
  109.    s[MIN(len, MAX_LEN - 1)] = '\0';
  110.    new = malloc(strlen(s)+1);
  111.    if (!new) 
  112.       return NULL;
  113.  
  114.    return strcpy(new, s);
  115.    }
  116.  
  117. /* byteskip(f, len)
  118.  * same as fseek, xcpt it works on stdin
  119.  */
  120. LOCAL void byteskip(f, len)
  121. struct exfile *f;
  122. int len;
  123.    {
  124.    int i;
  125.  
  126.    for (i = 0; i < len; i++)
  127.       getc_file(f);
  128.    }
  129.  
  130. /* v = getushort(f)
  131.  * reads an unsigned short from f
  132.  */
  133. LOCAL int getushort(f)
  134. struct exfile *f;
  135.    {
  136.    int i;
  137.  
  138.       /* order dependent !!! */
  139.    i = getc_file(f) << 8;
  140.    return i | getc_file(f);
  141.    }
  142.  
  143.  
  144. /* info = fill_sample_info(f):
  145.  * allocate and fill sample info with the information at current position of
  146.  * file f. Allocate memory for storing the sample, also.  fill_sample_info 
  147.  * is guaranteed to give you an accurate snapshot of what sample should 
  148.  * be like. In particular, length, rp_length, start, rp_start, fix_length, 
  149.  * fix_rp_length will have the values you can expect if part of the sample 
  150.  * is missing.
  151.  */
  152. LOCAL struct sample_info *fill_sample_info(f)
  153. struct exfile *f;
  154.    {
  155.     struct sample_info *info;
  156.  
  157.     info = malloc(sizeof(struct sample_info));
  158.     if (!info)
  159.         {
  160.         error = OUT_OF_MEM;
  161.         return empty_sample();
  162.         }
  163.     info->finetune = 0;
  164.     info->name = NULL;
  165.     info->length = 0;
  166.     info->start = NULL;
  167.     info->sample_size = 8;
  168.     info->rp_start = NULL;
  169.     info->fix_length = 0;
  170.     info->fix_rp_length = 0;
  171.  
  172.    info->name = getstring(f, SAMPLENAME_MAXLENGTH);
  173.    if (!info->name)
  174.       {
  175.       error = OUT_OF_MEM;
  176.       return NULL;
  177.       }
  178.    info->length = getushort(f);
  179.    info->finetune = getc_file(f);
  180.    if (info->finetune > 15)
  181.       info->finetune = 0;
  182.    info->volume = getc_file(f);
  183.    info->volume = MIN(info->volume, MAX_VOLUME);
  184.    info->rp_offset = getushort(f);
  185.    info->rp_length = getushort(f);
  186.  
  187.    /* the next check is for old modules for which
  188.     * the sample data types are a bit confused, so
  189.     * that what we were expecting to be #words is #bytes.
  190.     */
  191.       /* not sure I understand the -1 myself, though it's
  192.        * necessary to play kawai-k1 correctly 
  193.        */
  194.    if (info->rp_length + info->rp_offset - 1 > info->length)
  195.       info->rp_offset /= 2;
  196.     
  197.    if (info->rp_length + info->rp_offset > info->length)
  198.       info->rp_length = info->length - info->rp_offset;
  199.  
  200.    info->length *= 2;
  201.    info->rp_offset *= 2;
  202.    info->rp_length *= 2;
  203.       /* in all logic, a 2-sized sample could exist,
  204.        * but this is not the case, and even so, some
  205.        * trackers output empty instruments as being 2-sized.
  206.        */
  207.    if (info->length <= 2)
  208.       {
  209.         free(info);
  210.         return empty_sample();
  211.       }
  212.    else
  213.       {
  214.             /* add one byte for resampling */
  215.       info->start = (SAMPLE *)alloc_sample(info->length + 1);
  216.       if (!info->start)
  217.          {
  218.          error = OUT_OF_MEM;
  219.          return NULL;
  220.          }
  221.  
  222.       if (info->rp_length > 2)
  223.          info->rp_start = info->start + info->rp_offset;
  224.       else
  225.          {
  226.          info->rp_start = NULL;
  227.          info->rp_length = 0;
  228.          }
  229.         info->color = color++;
  230.         if (color > 15) color = 1;
  231.       }
  232.  
  233.    if (info->length > MAX_SAMPLE_LENGTH)
  234.       error = CORRUPT_FILE;
  235.    info->fix_length = int_to_fix(info->length);
  236.    info->fix_rp_length = int_to_fix(info->rp_length);
  237.     return info;
  238.    }
  239.  
  240. LOCAL void fill_song_info(info, f)
  241. struct song_info *info;
  242. struct exfile *f;
  243.    {
  244.    int i;
  245.    int p;
  246.  
  247.    info->length = getc_file(f);
  248.    getc_file(f);
  249.    info->maxpat = -1;
  250.    for (i = 0; i < NUMBER_PATTERNS; i++)
  251.       {
  252.       p = getc_file(f);
  253.       if (p >= NUMBER_PATTERNS)
  254.          p = 0;
  255.       if (p > info->maxpat)
  256.          info->maxpat = p;
  257.       info->patnumber[i] = p;
  258.       }
  259.    info->maxpat++;
  260.    if (info->maxpat == 0 || info->length == 0)
  261.       error = CORRUPT_FILE;
  262.    }
  263.  
  264. LOCAL void fill_event(e, f)
  265. struct event *e;
  266. struct exfile *f;
  267.    {
  268.    int a, b, c, d;
  269.  
  270.    a = getc_file(f);
  271.    b = getc_file(f);
  272.    c = getc_file(f);
  273.    d = getc_file(f);
  274.    e->sample_number = (a & 0x10) | (c >> 4);
  275.    e->effect = c & 0xf;
  276.    e->parameters = d;
  277.    switch(e->effect)
  278.       {
  279.    case EFF_EXTENDED:
  280.       e->effect = EXT_BASE + HI(e->parameters);
  281.       e->parameters = LOW(e->parameters);
  282.       break;
  283.    case 0:
  284.       e->effect = e->parameters ? EFF_ARPEGGIO : EFF_NONE;
  285.       break;
  286.    case EFF_SKIP:
  287.       e->parameters = HI(e->parameters) * 10 + LOW(e->parameters);
  288.       break;
  289.       }
  290.    e->pitch = ( (a & 15) << 8 ) | b;
  291.    e->note = find_note(e->pitch);
  292.    }
  293.  
  294. LOCAL void fill_pattern(pattern, f, song)
  295. struct block *pattern;
  296. struct exfile *f;
  297. struct song *song;
  298.    {
  299.    int i, j;
  300.     struct event *e;
  301.  
  302.     e = malloc(sizeof(struct event) * song->info.plength * song->ntracks);
  303.     if (!e)
  304.         {
  305.         error = OUT_OF_MEM;
  306.         return;
  307.         }
  308.     for (j = 0; j < song->ntracks; j++)
  309.         {
  310.         pattern->e[j] = e;
  311.         e += song->info.plength;
  312.         }
  313.    for (i = 0; i < song->info.plength; i++)
  314.       for (j = 0; j < song->ntracks; j++)
  315.          fill_event(&(pattern->e[j][i]), f);
  316.    }
  317.  
  318.  
  319. LOCAL void read_sample(info, f)
  320. struct sample_info *info;
  321. struct exfile *f;
  322.    {
  323.    if (info)
  324.       obtain_sample(info->start, info->length, f);
  325.    }
  326.  
  327.  
  328.  
  329.  
  330. /* new_song: allocate a new structure for a song.
  331.  *  clear each and every field as appropriate.
  332.  */
  333. LOCAL struct song *new_song()
  334.    {
  335.    struct song *new;
  336.     int i;
  337.  
  338.    new = (struct song *)malloc(sizeof(struct song));
  339.    if (!new) 
  340.       {
  341.       error = OUT_OF_MEM;
  342.       return NULL;
  343.       }
  344.    new->title = NULL;
  345.    new->info.length = 0;
  346.    new->info.maxpat = -1;
  347.    new->info.transpose = 0;
  348.    new->info.pblocks = NULL;
  349.     for (i = 1; i < MAX_NUMBER_SAMPLES; i++)
  350.         new->samples[i] = empty_sample();
  351.    return new;
  352.    }
  353.  
  354. /* release_song(song): give back all memory occupied by song. Assume 
  355.  * that each structure has been correctly allocated by a call to the
  356.  * corresponding new_XXX function.
  357.  */
  358. void release_song(song)
  359. struct song *song;
  360.    {
  361.    int i;
  362.  
  363.    if (!song)
  364.       return;
  365.    for (i = 1; i < MAX_NUMBER_SAMPLES; i++)
  366.       {
  367.         if (song->samples[i] != empty_sample())
  368.             {
  369.             if (song->samples[i]->start)
  370.                 free_sample(song->samples[i]->start);
  371.             if (song->samples[i]->name)
  372.                 free(song->samples[i]->name);
  373.             free(song->samples[i]);
  374.             }
  375.       }
  376.    if (song->info.pblocks)
  377.         {
  378.         for (i = 0; i < song->info.maxpat; i++)
  379.             if (song->info.pblocks[i].e[0])
  380.                 free(song->info.pblocks[i].e[0]);
  381.       free(song->info.pblocks);
  382.         }
  383.    if (song->title)
  384.       free(song->title);
  385.    free(song);
  386.    }
  387.  
  388. /* error_song(song): what we should return if there was an error. 
  389.  * Actually, is mostly useful for its side effects.
  390.  */
  391. LOCAL struct song *error_song(song)
  392. struct song *song;
  393.    {
  394.    release_song(song);
  395.    return NULL;
  396.    }
  397.  
  398. /* bad_sig(f, song): read the signature on file f and returns TRUE if 
  399.  * it is not a known sig. Set some parameters of song as a side effect
  400.  */
  401. LOCAL int bad_sig(f, song)
  402. struct exfile *f;
  403. struct song *song;
  404.    {
  405.    char a, b, c, d;
  406.  
  407.    a = getc_file(f);
  408.    b = getc_file(f);
  409.    c = getc_file(f);
  410.    d = getc_file(f);
  411.    if (a == 'M' && b == '.' && c == 'K' && d == '.')
  412.       return FALSE;
  413.    if (a == 'M' && b == '&' && c == 'K' && d == '!')
  414.       return FALSE;
  415.    if (a == 'F' && b == 'L' && c == 'T' && d == '4')
  416.       return FALSE;
  417.     if (a == '6' && b == 'C' && c == 'H' && d == 'N')
  418.         {
  419.         song->ntracks = 6;
  420.         return FALSE;
  421.         }
  422.     if (a == '8' && b == 'C' && c == 'H' && d == 'N')
  423.         {
  424.         song->ntracks= 8;
  425.         return FALSE;
  426.         }
  427.    return TRUE;
  428.    }
  429.  
  430. LOCAL void adjust_volumes(s)
  431. struct song *s;
  432.     {
  433.     int i, j;
  434.     for (i = 1; i <= s->ninstr; i++)
  435.         for (j = 0; j <= MAX_VOLUME; j++)        /* note <= not a bug */
  436.             s->samples[i]->volume_lookup[j] = (s->ntracks == 6) ? (4 * j) / 3 : j;
  437.     }
  438.  
  439.     
  440. /* s = read_song(f, type): try to read a song s of type NEW/OLD/NEW_NOCHECK
  441.  * from file f. Might fail, i.e., return NULL if file is not a mod file of 
  442.  * the correct type.
  443.  */
  444. struct song *read_song(f, type)
  445. struct exfile *f;
  446. int type;
  447.    {
  448.    struct song *song;
  449.    int i;
  450.  
  451.    error = NONE;
  452.  
  453.    song = new_song();
  454.    if (!song)
  455.       return error_song(song);
  456.  
  457.     song->ntracks = 4;
  458.    if (type == NEW || type == NEW_NO_CHECK)
  459.         {
  460.         song->type = PROTRACKER;
  461.       song->ninstr = PRO_NUMBER_SAMPLES;
  462.         }
  463.    else
  464.         {
  465.         song->type = OLD_ST;
  466.       song->ninstr = ST_NUMBER_SAMPLES;
  467.         }
  468.  
  469.     song->ntracks = NORMAL_NTRACKS;
  470.     song->info.plength = NORMAL_PLENGTH;
  471.  
  472.    song->title = getstring(f, TITLE_MAXLENGTH);
  473.    if (error != NONE)
  474.       return error_song(song);
  475.  
  476.     color = 1;        /* reset sample color to 1 for every song read */
  477.    for (i = 1; i <= song->ninstr; i++)
  478.       {
  479.         song->samples[i] = fill_sample_info(f);
  480.       if (error != NONE)
  481.          return error_song(song);
  482.       }
  483.  
  484.    fill_song_info(&song->info, f);
  485.  
  486.    if (error != NONE)
  487.       return error_song(song);
  488.  
  489.    if (type == NEW && bad_sig(f, song))
  490.       return error_song(song);
  491.  
  492.    if (type == NEW_NO_CHECK)
  493.       byteskip(f, 4);
  494.         
  495.       song->max_sample_width = 8;            /* temporary */
  496.  
  497.       if (song->ntracks == 4)
  498.           song->side_width = song->max_sample_width + 6 + 1;
  499.     else
  500.         song->side_width = song->max_sample_width + 6 + 2;
  501.  
  502.    song->info.pblocks = (struct block *)
  503.       malloc(sizeof(struct block) * song->info.maxpat);
  504.    if (!song->info.pblocks)
  505.       {
  506.       error = OUT_OF_MEM;
  507.       return error_song(song);
  508.       }
  509.     for (i = 0; i < song->info.maxpat; i++)
  510.         song->info.pblocks[i].e[0] = NULL;
  511.    for (i = 0; i < song->info.maxpat; i++)
  512.       {
  513.       fill_pattern(song->info.pblocks + i, f, song);
  514.       if (error != NONE)
  515.          return error_song(song);
  516.       }
  517.          /* future code... */
  518.    song->samples_start = tell_file(f);
  519.  
  520. #if 0
  521.    if (feof(f))
  522.       for (i = 1; i <= song->ninstr; i++)
  523.          find_sample(song->samples[i]);
  524.    else
  525. #endif
  526.       for (i = 1; i <= song->ninstr; i++)
  527.          read_sample(song->samples[i], f);
  528.     
  529.      adjust_volumes(song);
  530.    if (error != NONE)
  531.       return error_song(song);
  532.    return song;
  533.    }
  534.  
  535.